﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace CPN.PJN.Silverlight.UI.Interactions.Behaviors
{
    public class AutoCompleteBoxUpdateBehavior : Behavior<AutoCompleteBox>
    {
        public AutoCompleteBoxUpdateBehavior()
        {
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.TextChanged += OnTextChanged;
        }

        void OnTextChanged(object sender, RoutedEventArgs e)
        {
            var bindingExpr = AssociatedObject.GetBindingExpression(AutoCompleteBox.TextProperty);
            bindingExpr.UpdateSource();
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.TextChanged -= OnTextChanged;
        }
    }
}
